package com.hrm.controller;

import com.hrm.pojo.Users;
import com.hrm.service.UsersService;
import com.hrm.utils.JwtUtil;
import com.hrm.utils.entity.Result;
import com.hrm.utils.entity.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/users")
@Api(tags="员工模块接口")
public class UsersController {
    @Autowired
    private UsersService usersService;

    @Autowired
    private JwtUtil jwtUtil;

    @PostMapping("/reg")
    @ApiOperation(value = "员工注册")
    public Result registerUser(@RequestBody Users users)throws Exception{
        boolean flag=usersService.register(users);
        return new Result(flag,flag==true? StatusCode.SUCCESS:StatusCode.ERROR,"操作完成");
    }


    /**
     * 登录
     * @param users
     * @return
     * @throws Exception
     */
    @PostMapping("/login")
    @ApiOperation(value = "员工登录")
    public Result login(@RequestBody Users users)throws Exception{
        Users loginUsers= usersService.login(users.getLoginname(),users.getPassword());
        if(loginUsers==null){
            throw new Exception("登录失败");
        }
        //使用用户信息生成token
        String token = jwtUtil.createJWT(loginUsers.getId() + "", loginUsers.getLoginname(), loginUsers.getStatus() + "");      //后面加一个空的字符串就变成字符串了
        System.out.println("token:"+token);
        return new Result(true,StatusCode.SUCCESS,"操作完成",token);
    }
}
